//
//  IQLabelView.m
//  Created by kcandr on 17/12/14.

#import "IQLabelView.h"
#import <QuartzCore/QuartzCore.h>
#import "UITextField+DynamicFontSize.h"

CG_INLINE CGPoint CGRectGetCenter(CGRect rect)
{
    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
}

CG_INLINE CGRect CGRectScale(CGRect rect, CGFloat wScale, CGFloat hScale)
{
    return CGRectMake(rect.origin.x * wScale, rect.origin.y * hScale, rect.size.width * wScale, rect.size.height * hScale);
}

CG_INLINE CGFloat CGPointGetDistance(CGPoint point1, CGPoint point2)
{
    CGFloat fx = (point2.x - point1.x);
    CGFloat fy = (point2.y - point1.y);
    
    return sqrt((fx*fx + fy*fy));
}

CG_INLINE CGFloat CGAffineTransformGetAngle(CGAffineTransform t)
{
    return atan2(t.b, t.a);
}


CG_INLINE CGSize CGAffineTransformGetScale(CGAffineTransform t)
{
    return CGSizeMake(sqrt(t.a * t.a + t.c * t.c), sqrt(t.b * t.b + t.d * t.d)) ;
}

static IQLabelView *lastTouchedView;

@interface IQLabelView () <UIGestureRecognizerDelegate, UITextViewDelegate>

@end

@implementation IQLabelView
{
    CGFloat globalInset;
    
    CGRect initialBounds;
    CGFloat initialDistance;
    
    CGPoint beginningPoint;
    CGPoint beginningCenter;
    
    CGPoint touchLocation;
    
    CGFloat deltaAngle;
    CGRect beginBounds;
    
    CAShapeLayer *border;
    UITextView *labelTextField;
//    UIImageView *rotateView;
//    UIImageView *closeView;
    
//    BOOL isShowingEditingHandles;
}

@synthesize textColor, borderColor;
@synthesize fontName, fontSize;
@synthesize enableClose, enableRotate, enableMoveRestriction, showsContentShadow;
@synthesize delegate;
@synthesize closeImage, rotateImage;

- (void)refresh
{
    if (self.superview) {
        CGSize scale = CGAffineTransformGetScale(self.superview.transform);
        CGAffineTransform t = CGAffineTransformMakeScale(scale.width, scale.height);
        [closeView setTransform:CGAffineTransformInvert(t)];
        [rotateView setTransform:CGAffineTransformInvert(t)];
        
        if (isShowingEditingHandles) {
            [labelTextField.layer addSublayer:border];
        } else {
            [border removeFromSuperlayer];
        }
    }
}

-(void)didMoveToSuperview
{
    [super didMoveToSuperview];
    [self refresh];
}

- (void)setFrame:(CGRect)newFrame
{
    [super setFrame:newFrame];
    [self refresh];
}

- (id)initWithFrame:(CGRect)frame
{
    if (frame.size.width < 25)     frame.size.width = 25;
    if (frame.size.height < 25)    frame.size.height = 25;
    
    self = [super initWithFrame:frame];
    if (self) {
        globalInset = 12;
        
        self.backgroundColor = [UIColor clearColor];
        [self setAutoresizingMask:(UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth)];
        borderColor = [UIColor blackColor];
        
        labelTextField = [[UITextView alloc] initWithFrame:CGRectInset(self.bounds, globalInset, globalInset)];
        [labelTextField setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];
        [labelTextField setClipsToBounds:YES];
        labelTextField.delegate = self;
        labelTextField.backgroundColor = [UIColor clearColor];
        labelTextField.tintColor = [UIColor redColor];
        labelTextField.textColor = [UIColor whiteColor];
        labelTextField.text = @"";
        
        border = [CAShapeLayer layer];
        border.strokeColor = borderColor.CGColor;
        border.fillColor = nil;
//        border.lineDashPattern = @[@4, @3];
        border.lineDashPattern = nil;
        border.lineWidth = 3;
        
        [self insertSubview:labelTextField atIndex:0];
        
        closeView = [[UIImageView alloc] initWithFrame:CGRectMake(0-3, 0-3, globalInset * 2, globalInset * 2)];
        [closeView setAutoresizingMask:(UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin)];
        closeView.backgroundColor = [UIColor whiteColor];
        closeView.layer.cornerRadius = globalInset - 5;
        closeView.userInteractionEnabled = YES;
        [self addSubview:closeView];
        
        rotateView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width-globalInset*2 + 3, self.bounds.size.height-globalInset*2 + 3, globalInset*2, globalInset*2)];
        [rotateView setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin)];
        rotateView.backgroundColor = [UIColor whiteColor];
        rotateView.layer.cornerRadius = globalInset - 5;
        rotateView.contentMode = UIViewContentModeCenter;
        rotateView.userInteractionEnabled = YES;
        [self addSubview:rotateView];
        
        UIPanGestureRecognizer *moveGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveGesture:)];
        [self addGestureRecognizer:moveGesture];
        
        UITapGestureRecognizer *singleTapShowHide = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentTapped:)];
        [self addGestureRecognizer:singleTapShowHide];
        
        UITapGestureRecognizer *closeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTap:)];
        [closeView addGestureRecognizer:closeTap];
        
        UIPanGestureRecognizer *panRotateGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateViewPanGesture:)];
        [rotateView addGestureRecognizer:panRotateGesture];
        
        [moveGesture requireGestureRecognizerToFail:closeTap];
        
        [self setEnableMoveRestriction:NO];
        [self setEnableClose:YES];
        [self setEnableRotate:YES];
        [self setShowsContentShadow:YES];
        [self setCloseImage:[UIImage imageNamed:@"IQLabelView.bundle/sticker_close.png"]];
        [self setRotateImage:[UIImage imageNamed:@"IQLabelView.bundle/sticker_resize.png"]];
        
        [self showEditingHandles];
        [labelTextField becomeFirstResponder];
    }
    return self;
}

- (void)layoutSubviews
{
    if (labelTextField) {
        border.path = [UIBezierPath bezierPathWithRect:labelTextField.bounds].CGPath;
        border.frame = labelTextField.bounds;
    }
}

#pragma mark - Set Control Buttons

- (void)setEnableClose:(BOOL)value
{
    enableClose = value;
    [closeView setHidden:!enableClose];
    [closeView setUserInteractionEnabled:enableClose];
}

- (void)setEnableRotate:(BOOL)value
{
    enableRotate = value;
    [rotateView setHidden:!enableRotate];
    [rotateView setUserInteractionEnabled:enableRotate];
}

- (void)setShowsContentShadow:(BOOL)showShadow
{
    showsContentShadow = showShadow;
    
    if (showsContentShadow) {
        [self.layer setShadowColor:[UIColor blackColor].CGColor];
        [self.layer setShadowOffset:CGSizeMake(0, 5)];
        [self.layer setShadowOpacity:1.0];
        [self.layer setShadowRadius:4.0];
    } else {
        [self.layer setShadowColor:[UIColor clearColor].CGColor];
        [self.layer setShadowOffset:CGSizeZero];
        [self.layer setShadowOpacity:0.0];
        [self.layer setShadowRadius:0.0];
    }
}

- (void)setCloseImage:(UIImage *)image
{
    closeImage = image;
    [closeView setImage:closeImage];
}

- (void)setRotateImage:(UIImage *)image
{
    rotateImage = image;
    [rotateView setImage:rotateImage];
}

#pragma mark - Set Text Field

- (void)setFontName:(NSString *)name
{
    fontName = name;
    labelTextField.font = [UIFont fontWithName:fontName size:fontSize];
    //    [labelTextField adjustsWidthToFillItsContents];
}

- (void)setFontSize:(CGFloat)size
{
    fontSize = size;
    labelTextField.font = [UIFont fontWithName:fontName size:fontSize];
}

- (void)setTextColor:(UIColor *)color
{
    textColor = color;
    labelTextField.textColor = textColor;
}

- (void)setBorderColor:(UIColor *)color
{
    borderColor = color;
    border.strokeColor = borderColor.CGColor;
}

- (void)setTextAlpha:(CGFloat)alpha
{
    labelTextField.alpha = alpha;
}

- (CGFloat)textAlpha
{
    return labelTextField.alpha;
}

- (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder
{
    _attributedPlaceholder = attributedPlaceholder;
    //    [labelTextField setAttributedPlaceholder:attributedPlaceholder];
    //    [labelTextField adjustsWidthToFillItsContents];
}

#pragma mark - Bounds

- (void)hideEditingHandles
{
    lastTouchedView = nil;
    
    isShowingEditingHandles = NO;
    
    if (enableClose)       closeView.hidden = YES;
    if (enableRotate)      rotateView.hidden = YES;
    
    [labelTextField resignFirstResponder];
    
    [self refresh];
    
    if([delegate respondsToSelector:@selector(labelViewDidHideEditingHandles:)]) {
        [delegate labelViewDidHideEditingHandles:self];
    }
}

- (void)showEditingHandles
{
    [lastTouchedView hideEditingHandles];
    
    isShowingEditingHandles = YES;
    
    lastTouchedView = self;
    
    if (enableClose)       closeView.hidden = NO;
    if (enableRotate)      rotateView.hidden = NO;
    
    [self refresh];
    
    if([delegate respondsToSelector:@selector(labelViewDidShowEditingHandles:)]) {
        [delegate labelViewDidShowEditingHandles:self];
    }
}

#pragma mark - Gestures

- (void)contentTapped:(UITapGestureRecognizer*)tapGesture
{
    if (isShowingEditingHandles) {
        [self hideEditingHandles];
        [self.superview bringSubviewToFront:self];
    } else {
        [self showEditingHandles];
    }
}

- (void)closeTap:(UITapGestureRecognizer *)recognizer
{
    [self removeFromSuperview];
    
    if([delegate respondsToSelector:@selector(labelViewDidClose:)]) {
        [delegate labelViewDidClose:self];
    }
}

-(void)moveGesture:(UIPanGestureRecognizer *)recognizer
{
    if (!isShowingEditingHandles) {
        [self showEditingHandles];
    }
    touchLocation = [recognizer locationInView:self.superview];
    
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        beginningPoint = touchLocation;
        beginningCenter = self.center;
        
        [self setCenter:[self estimatedCenter]];
        beginBounds = self.bounds;
        
        if([delegate respondsToSelector:@selector(labelViewDidBeginEditing:)]) {
            [delegate labelViewDidBeginEditing:self];
        }
    } else if (recognizer.state == UIGestureRecognizerStateChanged) {
        [self setCenter:[self estimatedCenter]];
        
        if([delegate respondsToSelector:@selector(labelViewDidChangeEditing:)]) {
            [delegate labelViewDidChangeEditing:self];
        }
    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
        [self setCenter:[self estimatedCenter]];
        
        if([delegate respondsToSelector:@selector(labelViewDidEndEditing:)]) {
            [delegate labelViewDidEndEditing:self];
        }
    }
}

- (CGPoint)estimatedCenter
{
    CGPoint newCenter;
    CGFloat newCenterX = beginningCenter.x + (touchLocation.x - beginningPoint.x);
    CGFloat newCenterY = beginningCenter.y + (touchLocation.y - beginningPoint.y);
    if (enableMoveRestriction) {
        if (!(newCenterX - 0.5 * CGRectGetWidth(self.frame) > 0 &&
              newCenterX + 0.5 * CGRectGetWidth(self.frame) < CGRectGetWidth(self.superview.bounds))) {
            newCenterX = self.center.x;
        }
        if (!(newCenterY - 0.5 * CGRectGetHeight(self.frame) > 0 &&
              newCenterY + 0.5 * CGRectGetHeight(self.frame) < CGRectGetHeight(self.superview.bounds))) {
            newCenterY = self.center.y;
        }
        newCenter = CGPointMake(newCenterX, newCenterY);
    } else {
        newCenter = CGPointMake(newCenterX, newCenterY);
    }
    return newCenter;
}

- (void)rotateViewPanGesture:(UIPanGestureRecognizer *)recognizer
{
    touchLocation = [recognizer locationInView:self.superview];
    
    CGPoint center = CGRectGetCenter(self.frame);
    
    if ([recognizer state] == UIGestureRecognizerStateBegan) {
        deltaAngle = atan2(touchLocation.y-center.y, touchLocation.x-center.x)-CGAffineTransformGetAngle(self.transform);
        
        initialBounds = self.bounds;
        initialDistance = CGPointGetDistance(center, touchLocation);
        
        if([delegate respondsToSelector:@selector(labelViewDidBeginEditing:)]) {
            [delegate labelViewDidBeginEditing:self];
        }
    } else if ([recognizer state] == UIGestureRecognizerStateChanged) {
        float ang = atan2(touchLocation.y-center.y, touchLocation.x-center.x);
        
        float angleDiff = deltaAngle - ang;
//        [self setTransform:CGAffineTransformMakeRotation(-angleDiff)];
        [self setTransform:CGAffineTransformMakeRotation(0)];
        [self setNeedsDisplay];
        
        //Finding scale between current touchPoint and previous touchPoint
        double scale = sqrtf(CGPointGetDistance(center, touchLocation)/initialDistance);
        
        CGRect scaleRect = CGRectScale(initialBounds, scale, scale);
        
        if (scaleRect.size.width >= (1+globalInset*2 + 20) && scaleRect.size.height >= (1+globalInset*2 + 20)) {
            if (fontSize < 100 || CGRectGetWidth(scaleRect) < CGRectGetWidth(self.bounds)) {
                //                [labelTextField adjustsFontSizeToFillRect:scaleRect];
                [self setBounds:scaleRect];
            }
        }
        
        if([delegate respondsToSelector:@selector(labelViewDidChangeEditing:)]) {
            [delegate labelViewDidChangeEditing:self];
        }
    } else if ([recognizer state] == UIGestureRecognizerStateEnded) {
        if([delegate respondsToSelector:@selector(labelViewDidEndEditing:)]) {
            [delegate labelViewDidEndEditing:self];
        }
    }
}

/// 本view所在的vc
- (UIViewController *)selfViewController
{
    UIResponder *responder = self;

    do
    {
        if ([responder isKindOfClass:[UIViewController class]])
        {
            return (UIViewController *)responder;
        }

        responder = responder.nextResponder;
    }
    while (1);

    return nil;
}

#pragma mark - UITextView Delegate

//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
//{
//    [UIView animateWithDuration:0.3 animations:^{
//        UIViewController *vc = [self selfViewController];
//        CGRect frame =vc.view.frame;
//        frame.origin.y = 0.0;
//        vc.view.frame = frame;
//    }];
//
//    return YES;
//}
//
//- (void)textFieldDidBeginEditing:(UITextField *)textField
//{
//    if (_shouldViewMoveUp)
//    {
//        CGFloat offset = 216;
//
//        if (offset >= 0)
//        {
//            [UIView animateWithDuration:0.3 animations:^{
//                UIViewController *vc = [self selfViewController];
//
//                CGRect frame = vc.view.frame;
//                frame.origin.y = -offset;
//                vc.view.frame = frame;
//            }];
//        }
//    }
//
//    if([delegate respondsToSelector:@selector(labelViewDidStartEditing:)]) {
//        [delegate labelViewDidStartEditing:self];
//    }
//
//    [textView adjustsWidthToFillItsContents];
//}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
        if (_shouldViewMoveUp)
        {
            CGFloat offset = 216;
    
            if (offset >= 0)
            {
                [UIView animateWithDuration:0.3 animations:^{
                    UIViewController *vc = [self selfViewController];
    
                    CGRect frame = vc.view.frame;
                    frame.origin.y = -offset;
                    vc.view.frame = frame;
                }];
            }
        }
    
        if([delegate respondsToSelector:@selector(labelViewDidStartEditing:)]) {
            [delegate labelViewDidStartEditing:self];
        }
    
//        [textView adjustsWidthToFillItsContents];
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [UIView animateWithDuration:0.3 animations:^{
        UIViewController *vc = [self selfViewController];
        CGRect frame =vc.view.frame;
        frame.origin.y = 0.0;
        vc.view.frame = frame;
    }];

    return YES;
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if (isShowingEditingHandles) {
        return YES;
    }
    [self contentTapped:nil];
    return NO;
}

//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
//{
//    if (isShowingEditingHandles) {
//        return YES;
//    }
//    [self contentTapped:nil];
//    return NO;
//}

//- (void)textViewDidBeginEditing:(UITextView *)textView {
//    if([delegate respondsToSelector:@selector(labelViewDidStartEditing:)]) {
//        [delegate labelViewDidStartEditing:self];
//    }
//    
//    //    [textView adjustsWidthToFillItsContents];
//}

//- (void)textFieldDidBeginEditing:(UITextField *)textField
//{
//    if([delegate respondsToSelector:@selector(labelViewDidStartEditing:)]) {
//        [delegate labelViewDidStartEditing:self];
//    }
//
//    [textField adjustsWidthToFillItsContents];
//}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (!isShowingEditingHandles) {
        [self showEditingHandles];
    }
    //    [textField adjustsWidthToFillItsContents];
    return YES;
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if (!isShowingEditingHandles) {
//        [self showEditingHandles];
//    }
//    [textField adjustsWidthToFillItsContents];
//    return YES;
//}

@end


////
////  IQLabelView.m
////  Created by kcandr on 17/12/14.
//
//#import "IQLabelView.h"
//#import <QuartzCore/QuartzCore.h>
//#import "UITextField+DynamicFontSize.h"
//
//CG_INLINE CGPoint CGRectGetCenter(CGRect rect)
//{
//    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
//}
//
//CG_INLINE CGRect CGRectScale(CGRect rect, CGFloat wScale, CGFloat hScale)
//{
//    return CGRectMake(rect.origin.x * wScale, rect.origin.y * hScale, rect.size.width * wScale, rect.size.height * hScale);
//}
//
//CG_INLINE CGFloat CGPointGetDistance(CGPoint point1, CGPoint point2)
//{
//    //Saving Variables.
//    CGFloat fx = (point2.x - point1.x);
//    CGFloat fy = (point2.y - point1.y);
//    
//    return sqrt((fx*fx + fy*fy));
//}
//
//CG_INLINE CGFloat CGAffineTransformGetAngle(CGAffineTransform t)
//{
//    return atan2(t.b, t.a);
//}
//
//
//CG_INLINE CGSize CGAffineTransformGetScale(CGAffineTransform t)
//{
//    return CGSizeMake(sqrt(t.a * t.a + t.c * t.c), sqrt(t.b * t.b + t.d * t.d)) ;
//}
//
//static IQLabelView *lastTouchedView;
//
//@implementation IQLabelView
//{
//    CGFloat globalInset;
//    
//    CGRect initialBounds;
//    CGFloat initialDistance;
//    
//    CGPoint beginningPoint;
//    CGPoint beginningCenter;
//    
//    CGPoint prevPoint;
//    CGPoint touchLocation;
//    
//    CGFloat deltaAngle;
//    
//    CGAffineTransform startTransform;
//    CGRect beginBounds;
//    
//    CAShapeLayer *border;
//}
//
//@synthesize textColor = textColor, borderColor = borderColor;
//@synthesize fontName = fontName, fontSize = fontSize;
//@synthesize enableClose = enableClose, enableRotate = enableRotate;
//@synthesize delegate = delegate;
//@synthesize showContentShadow = showContentShadow;
//@synthesize closeImage = closeImage, rotateImage = rotateImage;
//
//- (void)refresh
//{
//    if (self.superview) {
//        CGSize scale = CGAffineTransformGetScale(self.superview.transform);
//        CGAffineTransform t = CGAffineTransformMakeScale(scale.width, scale.height);
//        [closeView setTransform:CGAffineTransformInvert(t)];
//        [rotateView setTransform:CGAffineTransformInvert(t)];
//        
//        if (isShowingEditingHandles) {
//            [textView.layer addSublayer:border];
//        } else {
//            [border removeFromSuperlayer];
//        }
//    }
//}
//
//-(void)didMoveToSuperview
//{
//    [super didMoveToSuperview];
//    [self refresh];
//}
//
//- (void)setFrame:(CGRect)newFrame
//{
//    [super setFrame:newFrame];
//    [self refresh];
//}
//
//- (id)initWithFrame:(CGRect)frame
//{
//    if (frame.size.width < (1+12*2))     frame.size.width = 25;
//    if (frame.size.height < (1+12*2))    frame.size.height = 25;
//    
//    self = [super initWithFrame:frame];
//    if (self) {
//        globalInset = 12;
//        
//        self.backgroundColor = [UIColor clearColor];
//        borderColor = [UIColor redColor];
//        
//        //Close button view which is in top left corner
//        closeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, globalInset*2, globalInset*2)];
//        [closeView setAutoresizingMask:(UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin)];
//        closeView.backgroundColor = [UIColor whiteColor];
//        closeView.layer.cornerRadius = globalInset - 5;
//        closeView.userInteractionEnabled = YES;
//        [self addSubview:closeView];
//        
//        //Rotating view which is in bottom right corner
//        rotateView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width-globalInset*2, self.bounds.size.height-globalInset*2, globalInset*2, globalInset*2)];
//        [rotateView setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin)];
//        rotateView.backgroundColor = [UIColor whiteColor];
//        rotateView.layer.cornerRadius = globalInset - 5;
//        rotateView.contentMode = UIViewContentModeCenter;
//        rotateView.userInteractionEnabled = YES;
//        [self addSubview:rotateView];
//        
//        UIPanGestureRecognizer *moveGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveGesture:)];
//        [self addGestureRecognizer:moveGesture];
//        
//        UITapGestureRecognizer *singleTapShowHide = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentTapped:)];
//        [self addGestureRecognizer:singleTapShowHide];
//        
//        UITapGestureRecognizer *closeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTap:)];
//        [closeView addGestureRecognizer:closeTap];
//        
//        UIPanGestureRecognizer *panRotateGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateViewPanGesture:)];
//        [rotateView addGestureRecognizer:panRotateGesture];
//        
//        [moveGesture requireGestureRecognizerToFail:closeTap];
//        
//        [self setEnableClose:YES];
//        [self setEnableRotate:YES];
//        [self setShowContentShadow:YES];
//        [self setCloseImage:[UIImage imageNamed:@"IQLabelView.bundle/sticker_close.png"]];
//        [self setRotateImage:[UIImage imageNamed:@"IQLabelView.bundle/sticker_resize.png"]];
//        
//        [self showEditingHandles];
//    }
//    return self;
//}
//
//- (void)layoutSubviews
//{
//    if (textView) {
//        border.path = [UIBezierPath bezierPathWithRect:textView.bounds].CGPath;
//        border.frame = textView.bounds;
//    }
//}
//
//#pragma mark - Set Control Buttons
//
//- (void)setEnableClose:(BOOL)value
//{
//    enableClose = value;
//    [closeView setHidden:!enableClose];
//    [closeView setUserInteractionEnabled:enableClose];
//}
//
//- (void)setEnableRotate:(BOOL)value
//{
//    enableRotate = value;
//    [rotateView setHidden:!enableRotate];
//    [rotateView setUserInteractionEnabled:enableRotate];
//}
//
//- (void)setShowContentShadow:(BOOL)showShadow
//{
//    showContentShadow = showShadow;
//    
//    if (showContentShadow) {
//        [self.layer setShadowColor:[UIColor blackColor].CGColor];
//        [self.layer setShadowOffset:CGSizeMake(0, 5)];
//        [self.layer setShadowOpacity:1.0];
//        [self.layer setShadowRadius:4.0];
//    } else {
//        [self.layer setShadowColor:[UIColor clearColor].CGColor];
//        [self.layer setShadowOffset:CGSizeZero];
//        [self.layer setShadowOpacity:0.0];
//        [self.layer setShadowRadius:0.0];
//    }
//}
//
//- (void)setCloseImage:(UIImage *)image
//{
//    closeImage = image;
//    [closeView setImage:closeImage];
//}
//
//- (void)setRotateImage:(UIImage *)image
//{
//    rotateImage = image;
//    [rotateView setImage:rotateImage];
//}
//
//#pragma mark - Set Text Field
//
//- (void)setTextField:(UITextField *)field
//{
//    [textView removeFromSuperview];
//    
//    textView = field;
//    
//    textView.frame = CGRectInset(self.bounds, globalInset, globalInset);
//    
//    [textView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];
//    textView.delegate = self;
//    textView.backgroundColor = [UIColor clearColor];
//    textView.tintColor = [UIColor redColor];
//    [textView becomeFirstResponder];
//    
//    border = [CAShapeLayer layer];
//    border.strokeColor = borderColor.CGColor;
//    border.fillColor = nil;
//    border.lineDashPattern = @[@4, @3];
//    
//    [self insertSubview:textView atIndex:0];
//}
//
//- (void)setFontName:(NSString *)name
//{
//    fontName = name;
//    textView.font = [UIFont fontWithName:fontName size:fontSize];
//    [textView adjustsWidthToFillItsContents];
//}
//
//- (void)setFontSize:(CGFloat)size
//{
//    fontSize = size;
//    textView.font = [UIFont fontWithName:fontName size:fontSize];
//}
//
//- (void)setTextColor:(UIColor *)color
//{
//    textColor = color;
//    textView.textColor = textColor;
//}
//
//- (void)setBorderColor:(UIColor *)color
//{
//    borderColor = color;
//    border.strokeColor = borderColor.CGColor;
//}
//
//#pragma mark - Bounds
//
//- (void)hideEditingHandles
//{
//    lastTouchedView = nil;
//    
//    isShowingEditingHandles = NO;
//    
//    if (enableClose)       closeView.hidden = YES;
//    if (enableRotate)      rotateView.hidden = YES;
//    
//    [textView resignFirstResponder];
//    
//    [self refresh];
//    
//    if([delegate respondsToSelector:@selector(labelViewDidHideEditingHandles:)]) {
//        [delegate labelViewDidHideEditingHandles:self];
//    }
//}
//
//- (void)showEditingHandles
//{
//    [lastTouchedView hideEditingHandles];
//    
//    isShowingEditingHandles = YES;
//    
//    lastTouchedView = self;
//    
//    if (enableClose)       closeView.hidden = NO;
//    if (enableRotate)      rotateView.hidden = NO;
//    
//    [self refresh];
//    
//    if([delegate respondsToSelector:@selector(labelViewDidShowEditingHandles:)]) {
//        [delegate labelViewDidShowEditingHandles:self];
//    }
//}
//
//#pragma mark - Gestures
//
//- (void)contentTapped:(UITapGestureRecognizer*)tapGesture
//{
//    if (isShowingEditingHandles) {
//        [self hideEditingHandles];
//        [self.superview bringSubviewToFront:self];
//    } else {
//        [self showEditingHandles];
//    }
//}
//
//- (void)closeTap:(UITapGestureRecognizer *)recognizer
//{
//    [self removeFromSuperview];
//    
//    if([delegate respondsToSelector:@selector(labelViewDidClose:)]) {
//        [delegate labelViewDidClose:self];
//    }
//}
//
//-(void)moveGesture:(UIPanGestureRecognizer *)recognizer
//{
//    if (!isShowingEditingHandles) {
//        [self showEditingHandles];
//    }
//    touchLocation = [recognizer locationInView:self.superview];
//    
//    if (recognizer.state == UIGestureRecognizerStateBegan) {
//        beginningPoint = touchLocation;
//        beginningCenter = self.center;
//        
//        [self setCenter:CGPointMake(beginningCenter.x+(touchLocation.x-beginningPoint.x), beginningCenter.y+(touchLocation.y-beginningPoint.y))];
//        
//        beginBounds = self.bounds;
//        
//        if([delegate respondsToSelector:@selector(labelViewDidBeginEditing:)]) {
//            [delegate labelViewDidBeginEditing:self];
//        }
//    } else if (recognizer.state == UIGestureRecognizerStateChanged) {
//        [self setCenter:CGPointMake(beginningCenter.x+(touchLocation.x-beginningPoint.x), beginningCenter.y+(touchLocation.y-beginningPoint.y))];
//        
//        if([delegate respondsToSelector:@selector(labelViewDidChangeEditing:)]) {
//            [delegate labelViewDidChangeEditing:self];
//        }
//    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
//        [self setCenter:CGPointMake(beginningCenter.x+(touchLocation.x-beginningPoint.x), beginningCenter.y+(touchLocation.y-beginningPoint.y))];
//        
//        if([delegate respondsToSelector:@selector(labelViewDidEndEditing:)]) {
//            [delegate labelViewDidEndEditing:self];
//        }
//    }
//    
//    prevPoint = touchLocation;
//}
//
//- (void)rotateViewPanGesture:(UIPanGestureRecognizer *)recognizer
//{
//    touchLocation = [recognizer locationInView:self.superview];
//    
//    CGPoint center = CGRectGetCenter(self.frame);
//    
//    if ([recognizer state] == UIGestureRecognizerStateBegan) {
//        deltaAngle = atan2(touchLocation.y-center.y, touchLocation.x-center.x)-CGAffineTransformGetAngle(self.transform);
//        
//        initialBounds = self.bounds;
//        initialDistance = CGPointGetDistance(center, touchLocation);
//        
//        if([delegate respondsToSelector:@selector(labelViewDidBeginEditing:)]) {
//            [delegate labelViewDidBeginEditing:self];
//        }
//    } else if ([recognizer state] == UIGestureRecognizerStateChanged) {
//        float ang = atan2(touchLocation.y-center.y, touchLocation.x-center.x);
//        
//        float angleDiff = deltaAngle - ang;
//        [self setTransform:CGAffineTransformMakeRotation(-angleDiff)];
//        [self setNeedsDisplay];
//        
//        //Finding scale between current touchPoint and previous touchPoint
//        double scale = sqrtf(CGPointGetDistance(center, touchLocation)/initialDistance);
//        
//        CGRect scaleRect = CGRectScale(initialBounds, scale, scale);
//        
//        if (scaleRect.size.width >= (1+globalInset*2 + 20) && scaleRect.size.height >= (1+globalInset*2 + 20)) {
//            if (fontSize < 100 || CGRectGetWidth(scaleRect) < CGRectGetWidth(self.bounds)) {
//                [textView adjustsFontSizeToFillRect:scaleRect];
//                [self setBounds:scaleRect];
//            }
//        }
//        
//        if([delegate respondsToSelector:@selector(labelViewDidChangeEditing:)]) {
//            [delegate labelViewDidChangeEditing:self];
//        }
//    } else if ([recognizer state] == UIGestureRecognizerStateEnded) {
//        if([delegate respondsToSelector:@selector(labelViewDidEndEditing:)]) {
//            [delegate labelViewDidEndEditing:self];
//        }
//    }
//}
//
//#pragma mark - UITextField Delegate
//
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
//{
////    if (_shouldViewMoveUp)
////    {
////        CGFloat offset = 216;
////        
////        if (offset >= 0)
////        {
////            [UIView animateWithDuration:0.3 animations:^{
////                UIViewController *vc = [self selfViewController];
////                
////                CGRect frame = vc.view.frame;
////                frame.origin.y = -offset;
////                vc.view.frame = frame;
////            }];
////        }
////    }
//    
//    if (isShowingEditingHandles) {
//        return YES;
//    }
//    [self contentTapped:nil];
//    return NO;
//}
//
//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
//{
//    [UIView animateWithDuration:0.3 animations:^{
//        UIViewController *vc = [self selfViewController];
//        CGRect frame =vc.view.frame;
//        frame.origin.y = 0.0;
//        vc.view.frame = frame;
//    }];
//
//    return YES;
//}
//
//- (void)textFieldDidBeginEditing:(UITextField *)textField
//{
//    if (_shouldViewMoveUp)
//    {
//        CGFloat offset = 216;
//        
//        if (offset >= 0)
//        {
//            [UIView animateWithDuration:0.3 animations:^{
//                UIViewController *vc = [self selfViewController];
//                
//                CGRect frame = vc.view.frame;
//                frame.origin.y = -offset;
//                vc.view.frame = frame;
//            }];
//        }
//    }
//    
//    if([delegate respondsToSelector:@selector(labelViewDidStartEditing:)]) {
//        [delegate labelViewDidStartEditing:self];
//    }
//    
//    [textView adjustsWidthToFillItsContents];
//}
//
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if (!isShowingEditingHandles) {
//        [self showEditingHandles];
//    }
//    [textView adjustsWidthToFillItsContents];
//    return YES;
//}
//
///// 本view所在的vc
//- (UIViewController *)selfViewController
//{
//    UIResponder *responder = self;
//
//    do
//    {
//        if ([responder isKindOfClass:[UIViewController class]])
//        {
//            return (UIViewController *)responder;
//        }
//
//        responder = responder.nextResponder;
//    }
//    while (1);
//
//    return nil;
//}
//
//@end
//
//
//////
//////  IQLabelView.m
//////  Created by kcandr on 17/12/14.
////
////#import "IQLabelView.h"
////#import <QuartzCore/QuartzCore.h>
////#import "UITextField+DynamicFontSize.h"
////
////CG_INLINE CGPoint CGRectGetCenter(CGRect rect)
////{
////    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
////}
////
////CG_INLINE CGRect CGRectScale(CGRect rect, CGFloat wScale, CGFloat hScale)
////{
////    return CGRectMake(rect.origin.x * wScale, rect.origin.y * hScale, rect.size.width * wScale, rect.size.height * hScale);
////}
////
////CG_INLINE CGFloat CGPointGetDistance(CGPoint point1, CGPoint point2)
////{
////    //Saving Variables.
////    CGFloat fx = (point2.x - point1.x);
////    CGFloat fy = (point2.y - point1.y);
////    
////    return sqrt((fx*fx + fy*fy));
////}
////
////CG_INLINE CGFloat CGAffineTransformGetAngle(CGAffineTransform t)
////{
////    return atan2(t.b, t.a);
////}
////
////
////CG_INLINE CGSize CGAffineTransformGetScale(CGAffineTransform t)
////{
////    return CGSizeMake(sqrt(t.a * t.a + t.c * t.c), sqrt(t.b * t.b + t.d * t.d)) ;
////}
////
////
////static IQLabelView *lastTouchedView;
////
////@implementation IQLabelView
////{
////    CGFloat _globalInset;
////
////    CGRect initialBounds;
////    CGFloat initialDistance;
////
////    CGPoint beginningPoint;
////    CGPoint beginningCenter;
////
////    CGPoint prevPoint;
////    CGPoint touchLocation;
////    
////    CGFloat deltaAngle;
////    
////    CGAffineTransform startTransform;
////    CGRect beginBounds;
////    
////    CAShapeLayer *border;
////}
////
////@synthesize textView = _textView;
////@synthesize fontName = _fontName, fontSize = _fontSize;
////@synthesize enableClose = _enableClose;
////@synthesize enableRotate = _enableRotate;
////@synthesize delegate = _delegate;
////@synthesize showContentShadow = _showContentShadow;
////@synthesize closeImage = _closeImage, rotateImage = _rotateImage;
////
////-(void)refresh
////{
////    if (self.superview) {
////        CGSize scale = CGAffineTransformGetScale(self.superview.transform);
////        CGAffineTransform t = CGAffineTransformMakeScale(scale.width, scale.height);
////        [closeView setTransform:CGAffineTransformInvert(t)];
////        [rotateView setTransform:CGAffineTransformInvert(t)];
////        
////        if (_isShowingEditingHandles) {
////            [_textView.layer addSublayer:border];
////        } else {
////            [border removeFromSuperlayer];
////        }
////    }
////}
////
////-(void)didMoveToSuperview
////{
////    [super didMoveToSuperview];
////    [self refresh];
////}
////
////- (void)setFrame:(CGRect)newFrame
////{
////    [super setFrame:newFrame];
////    [self refresh];
////}
////
////- (id)initWithFrame:(CGRect)frame
////{
////    /*(1+_globalInset*2)*/
////    if (frame.size.width < (1+12*2))     frame.size.width = 25;
////    if (frame.size.height < (1+12*2))   frame.size.height = 25;
//// 
////    self = [super initWithFrame:frame];
////    if (self) {
////        _globalInset = 12;
////        
////        //        self = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
////        self.backgroundColor = [UIColor clearColor];
////        
////        //Close button view which is in top left corner
////        closeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _globalInset*2, _globalInset*2)];
////        [closeView setAutoresizingMask:(UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin)];
////        closeView.backgroundColor = [UIColor whiteColor];
////        closeView.layer.cornerRadius = _globalInset - 5;
////        closeView.userInteractionEnabled = YES;
////        [self addSubview:closeView];
////        
////         //Rotating view which is in bottom right corner
////        okView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width-_globalInset*2, 0, _globalInset*2, _globalInset*2)];
////        [okView setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin)];
////        okView.backgroundColor = [UIColor whiteColor];
////        okView.layer.cornerRadius = _globalInset - 5;
////        okView.contentMode = UIViewContentModeCenter;
////        okView.userInteractionEnabled = YES;
////        [self addSubview:okView];
////        
////        
////        rotateView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width-_globalInset*2, self.bounds.size.height-_globalInset*2, _globalInset*2, _globalInset*2)];
////        [rotateView setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin)];
////        rotateView.backgroundColor = [UIColor whiteColor];
////        rotateView.layer.cornerRadius = _globalInset - 5;
////        rotateView.contentMode = UIViewContentModeCenter;
////        rotateView.userInteractionEnabled = YES;
////        [self addSubview:rotateView];
////        
////
////        UIPanGestureRecognizer *moveGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveGesture:)];
////        [self addGestureRecognizer:moveGesture];
////        
////        UITapGestureRecognizer *singleTapShowHide = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentTapped:)];
////        [self addGestureRecognizer:singleTapShowHide];
////        
////        UITapGestureRecognizer *closeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeTap:)];
////        [closeView addGestureRecognizer:closeTap];
////        
////        UITapGestureRecognizer *okTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(okTap:)];
////        [okView addGestureRecognizer:okTap];
////        
////        UIPanGestureRecognizer *panRotateGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateViewPanGesture:)];
////        [rotateView addGestureRecognizer:panRotateGesture];
////        
////        [moveGesture requireGestureRecognizerToFail:closeTap];
////        
////        [self setEnableClose:YES];
////        [self setEnableRotate:YES];
////        [self setShowContentShadow:YES];
////        [self setCloseImage:[UIImage imageNamed:@"IQLabelView.bundle/sticker_close.png"]];
////        [self setRotateImage:[UIImage imageNamed:@"IQLabelView.bundle/sticker_resize.png"]];
////        [self setOkImage:[UIImage imageNamed:@"IQLabelView.bundle/sticker_ok.png"]];
////        
////        [self hideEditingHandles];
////     }
////    return self;
////}
////
////- (void)layoutSubviews
////{
////    if (_textView) {
////        border.path = [UIBezierPath bezierPathWithRect:_textView.bounds].CGPath;
////        border.frame = _textView.bounds;
////    }
////}
////
////#pragma mark - Set Control Buttons
////
////- (void)setEnableClose:(BOOL)enableClose
////{
////    _enableClose = enableClose;
////    [closeView setHidden:!_enableClose];
////    [closeView setUserInteractionEnabled:_enableClose];
////}
////
////- (void)setEnableRotate:(BOOL)enableRotate
////{
////    _enableRotate = enableRotate;
////    [rotateView setHidden:!_enableRotate];
////    [rotateView setUserInteractionEnabled:_enableRotate];
////}
////
////- (void)setShowContentShadow:(BOOL)showContentShadow
////{
////    _showContentShadow = showContentShadow;
////    
////    if (_showContentShadow) {
////        [self.layer setShadowColor:[UIColor blackColor].CGColor];
////        [self.layer setShadowOffset:CGSizeMake(0, 5)];
////        [self.layer setShadowOpacity:1.0];
////        [self.layer setShadowRadius:4.0];
////    } else {
////        [self.layer setShadowColor:[UIColor clearColor].CGColor];
////        [self.layer setShadowOffset:CGSizeZero];
////        [self.layer setShadowOpacity:0.0];
////        [self.layer setShadowRadius:0.0];
////    }
////}
////
////- (void)setCloseImage:(UIImage *)closeImage
////{
////    _closeImage = closeImage;
////    [closeView setImage:_closeImage];
////}
////
////- (void)setRotateImage:(UIImage *)rotateImage
////{
////    _rotateImage = rotateImage;
////    [rotateView setImage:_rotateImage];
////}
////
////- (void)setOkImage:(UIImage *)okImage
////{
////    _okImage = okImage;
////    [okView setImage:_okImage];
////}
////
////#pragma mark - Set Text Field
////
////- (void)setTextView:(UITextField *)textView
////{
////    [_textView removeFromSuperview];
////    
////    _textView = textView;
////    
////    _textView.frame = CGRectInset(self.bounds, _globalInset, _globalInset);
////    
////    [_textView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];
////    _textView.delegate = self;
////    _textView.backgroundColor = [UIColor clearColor];
////    _textView.tintColor = [UIColor redColor];
////    [_textView becomeFirstResponder];
////    
////    border = [CAShapeLayer layer];
////    border.strokeColor = [UIColor redColor].CGColor;
////    border.fillColor = nil;
////    border.lineDashPattern = @[@4, @3];
////    
////    [self insertSubview:_textView atIndex:0];
////}
////
////- (void)setFontName:(NSString *)fontName
////{
////    _fontName = fontName;
////    _textView.font = [UIFont fontWithName:_fontName size:_fontSize];
////    [_textView adjustsWidthToFillItsContents];
////}
////
////- (void)setFontSize:(CGFloat)fontSize
////{
////    _fontSize = fontSize;
////    _textView.font = [UIFont fontWithName:_fontName size:_fontSize];
////}
////
////#pragma mark - Bounds
////
////- (void)hideEditingHandles
////{
////    lastTouchedView = nil;
////    okView.hidden = YES;
////    _isShowingEditingHandles = NO;
////    
////    if (_enableClose)       closeView.hidden = YES;
////    if (_enableRotate)      rotateView.hidden = YES;
////    
////    [_textView resignFirstResponder];
////    
////    [self refresh];
////    
////    if([_delegate respondsToSelector:@selector(labelViewDidHideEditingHandles:)]) {
////        [_delegate labelViewDidHideEditingHandles:self];
////    }
////}
////
////- (void)showEditingHandles
////{
////    [lastTouchedView hideEditingHandles];
////    
////    _isShowingEditingHandles = YES;
////    
////    lastTouchedView = self;
////    
////    if (_enableClose)       closeView.hidden = NO;
////    if (_enableRotate)      rotateView.hidden = NO;
////    
////    [self refresh];
////    
////    if([_delegate respondsToSelector:@selector(labelViewDidShowEditingHandles:)]) {
////        [_delegate labelViewDidShowEditingHandles:self];
////    }
////}
////
////#pragma mark - Gestures
////
////- (void)contentTapped:(UITapGestureRecognizer*)tapGesture
////{
////    okView.hidden = NO;
////    if (_isShowingEditingHandles) {
////        [self hideEditingHandles];
////        [self.superview bringSubviewToFront:self];
////    } else {
////        [self showEditingHandles];
////    }
////}
////
////- (void)closeTap:(UITapGestureRecognizer *)recognizer
////{
////    [self removeFromSuperview];
////    
////    if([_delegate respondsToSelector:@selector(labelViewDidClose:)]) {
////        [_delegate labelViewDidClose:self];
////    }
////}
////
////- (void)okTap:(UITapGestureRecognizer *)recognizer
////{
////    [self hideEditingHandles];
////    okView.hidden = YES;
////}
////
////-(void)moveGesture:(UIPanGestureRecognizer *)recognizer
////{
////    okView.hidden = NO;
////    if (!_isShowingEditingHandles) {
////        [self showEditingHandles];
////    }
////    touchLocation = [recognizer locationInView:self.superview];
////    
////    if (recognizer.state == UIGestureRecognizerStateBegan) {
////        beginningPoint = touchLocation;
////        beginningCenter = self.center;
//// 
////        [self setCenter:CGPointMake(beginningCenter.x+(touchLocation.x-beginningPoint.x), beginningCenter.y+(touchLocation.y-beginningPoint.y))];
////
////        beginBounds = self.bounds;
////        
////        if([_delegate respondsToSelector:@selector(labelViewDidBeginEditing:)]) {
////            [_delegate labelViewDidBeginEditing:self];
////        }
////    } else if (recognizer.state == UIGestureRecognizerStateChanged) {
////        [self setCenter:CGPointMake(beginningCenter.x+(touchLocation.x-beginningPoint.x), beginningCenter.y+(touchLocation.y-beginningPoint.y))];
////        
////        if([_delegate respondsToSelector:@selector(labelViewDidChangeEditing:)]) {
////            [_delegate labelViewDidChangeEditing:self];
////        }
////    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
////        [self setCenter:CGPointMake(beginningCenter.x+(touchLocation.x-beginningPoint.x), beginningCenter.y+(touchLocation.y-beginningPoint.y))];
////    
////        if([_delegate respondsToSelector:@selector(labelViewDidEndEditing:)]) {
////            [_delegate labelViewDidEndEditing:self];
////        }
////    }
////
////    prevPoint = touchLocation;
////}
////
////- (void)rotateViewPanGesture:(UIPanGestureRecognizer *)recognizer
////{
////    touchLocation = [recognizer locationInView:self.superview];
////    
////    CGPoint center = CGRectGetCenter(self.frame);
////    
////    if ([recognizer state] == UIGestureRecognizerStateBegan) {
////        deltaAngle = atan2(touchLocation.y-center.y, touchLocation.x-center.x)-CGAffineTransformGetAngle(self.transform);
////        
////        initialBounds = self.bounds;
////        initialDistance = CGPointGetDistance(center, touchLocation);
////       
////        if([_delegate respondsToSelector:@selector(labelViewDidBeginEditing:)]) {
////            [_delegate labelViewDidBeginEditing:self];
////        }
////    } else if ([recognizer state] == UIGestureRecognizerStateChanged) {
////        float ang = atan2(touchLocation.y-center.y, touchLocation.x-center.x);
////        
////        float angleDiff = deltaAngle - ang;
//////        float angleDiff = -ang;
////        [self setTransform:CGAffineTransformMakeRotation(-angleDiff)];
////        [self setNeedsDisplay];
////        
////        //Finding scale between current touchPoint and previous touchPoint
////        double scale = sqrtf(CGPointGetDistance(center, touchLocation)/initialDistance);
////        
////        CGRect scaleRect = CGRectScale(initialBounds, scale, scale);
//// 
////        if (scaleRect.size.width >= (1+_globalInset*2 + 20) && scaleRect.size.height >= (1+_globalInset*2 + 20)) {
////            if (_fontSize < 100 || CGRectGetWidth(scaleRect) < CGRectGetWidth(self.bounds)) {
////                [_textView adjustsFontSizeToFillRect:scaleRect];
////                [self setBounds:scaleRect];
////            }
////        }
////        
////        if([_delegate respondsToSelector:@selector(labelViewDidChangeEditing:)]) {
////            [_delegate labelViewDidChangeEditing:self];
////        }
////    } else if ([recognizer state] == UIGestureRecognizerStateEnded) {
////        if([_delegate respondsToSelector:@selector(labelViewDidEndEditing:)]) {
////            [_delegate labelViewDidEndEditing:self];
////        }
////    }
////}
////
////
/////// 本view所在的vc
////- (UIViewController *)selfViewController
////{
////    UIResponder *responder = self;
////    
////    do
////    {
////        if ([responder isKindOfClass:[UIViewController class]])
////        {
////            return (UIViewController *)responder;
////        }
////        
////        responder = responder.nextResponder;
////    }
////    while (1);
////    
////    return nil;
////}
////
////#pragma mark - UITextField Delegate
////
////- (void)textFieldDidBeginEditing:(UITextField *)textField
////{
////    if([_delegate respondsToSelector:@selector(labelViewDidStartEditing:)]) {
////        [_delegate labelViewDidStartEditing:self];
////        
////    }
////    [self contentTapped:nil];
////    [_textView adjustsWidthToFillItsContents];
////}
////
////- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
////{
////    if (_shouldViewMoveUp)
////    {
////        CGFloat offset = 216;
////        
////        if (offset >= 0)
////        {
////            [UIView animateWithDuration:0.3 animations:^{
////                UIViewController *vc = [self selfViewController];
////                
////                CGRect frame = vc.view.frame;
////                frame.origin.y = -offset;
////                vc.view.frame = frame;
////            }];
////        }
////    }
////    
////    
////    return YES;
////}
////
////- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
////{
////
////    
////    [UIView animateWithDuration:0.3 animations:^{
////        UIViewController *vc = [self selfViewController];
////        CGRect frame =vc.view.frame;
////        frame.origin.y = 0.0;
////        vc.view.frame = frame;
////    }];
////    
////    return YES;
////}
////
////
////- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
////{
////    if (!_isShowingEditingHandles) {
////        [self showEditingHandles];
////    }
////    [_textView adjustsWidthToFillItsContents];
////    return YES;
////}
////
////@end
////
////// 版权属于原作者
////// http://code4app.com (cn) http://code4app.net (en)
////// 发布代码于最专业的源码分享网站: Code4App.com 
